<?php

class StopController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $stops = new Application_Model_DbTable_Stops();
		$this->view->busstop = $stops->fetchAll();
    }

    public function addstopAction()
    {
        $form = new Application_Form_Stop();
		$form->submit->setLabel('Add');
		$this->view->form = $form;
		
		if ($this->getRequest()->isPost()) {
				$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)) {
					$busstops = new Application_Model_DbTable_Stops();
					$busstops->addBusstop($form->getValue('name'),$form->getValue('latitude'),$form->getValue('longitude'));
					$this->_helper->redirector('index');
			} 
				else {
					$form->populate($formData);
				}
		}	
	}

    public function editstopAction()
    {
        $form = new Application_Form_Stop();
		$form->submit->setLabel('Save');
		$this->view->form = $form;
		
			if ($this->getRequest()->isPost()) {
					$formData = $this->getRequest()->getPost();
				if ($form->isValid($formData)) {
						$busstops = new Application_Model_DbTable_Stops();
						$busstops->updateBusstop((int)$form->getValue('id'),$form->getValue('name'), $form->getValue('latitude'), $form->getValue('longitude'));
						$this->_helper->redirector('index');
				}
					else {
						$form->populate($formData);
					}
			} 
				else {
					$id = $this->_getParam('id', null);
						if ($id != null) {
							$busstops = new Application_Model_DbTable_Stops();
							$form->populate($busstops->getBusstop($id));
						}
				}
	}

    public function deletestopAction()
    {
        if ($this->getRequest()->isPost()) {
				$del = $this->getRequest()->getPost('del');
			if ($del == 'Yes') {
					$id = $this->getRequest()->getPost('id');
					$busstops = new Application_Model_DbTable_Stops();
					$busstops->deleteBusstop($id);
			}
				$this->_helper->redirector('index');
		} 
			else {
				$id = $this->_getParam('id', null);
				$busstops = new Application_Model_DbTable_Stops();
				$this->view->busstop = $busstops->getBusstop($id);
			}
    }


}







